/**
 * @file 
 * @author 蔡云蔚 (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-08-20
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include "my_debug.h"

#ifdef MY_USE_CMSIS_OS
#include "my_mutex.h"
#endif



My_Debug My_Debug::only;//初始化静态成员

My_Debug& My_Debug::getInstance()
{
    return only;
}

My_Debug& my_debug = My_Debug::getInstance();


void My_Debug::init(void)
{
    //串口
    THE_DEBUG_UART_INIT();
    
}

HAL_StatusTypeDef My_Debug::transmit(const uint8_t* pData, uint16_t Size, uint32_t Timeout)
{
    Transmitted = false;
    HAL_StatusTypeDef result = HAL_UART_Transmit(&THE_DEBUG_UART_HAND, pData, Size, Timeout);
    if(result == HAL_OK)
    {
        Transmitted = true;
    }
    return result;
}

HAL_StatusTypeDef My_Debug::transmit_IT(const uint8_t *pData, uint16_t Size)
{
    Transmitted = false;
    return HAL_UART_Transmit_IT(&THE_DEBUG_UART_HAND, pData, Size);
}

HAL_StatusTypeDef My_Debug::transmit_DMA(const uint8_t* pData, uint16_t Size)
{
    // while(Transmitted != true){};//等待上次DMA发送结束
    Transmitted = false;
    HAL_StatusTypeDef result = HAL_UART_Transmit_DMA(&THE_DEBUG_UART_HAND, pData, Size);
    return result;
}


HAL_StatusTypeDef My_Debug::receive(uint16_t Size, uint32_t Timeout)
{
    ReCount = 0;
    Received = false;
    HAL_StatusTypeDef result = HAL_UART_Receive(&THE_DEBUG_UART_HAND, ReBuffer, Size, Timeout);
    if(result == HAL_OK)
    {
        ReCount = Size;
        Received = true;
    }
    return result;
}

HAL_StatusTypeDef My_Debug::receive_IT(uint16_t Size)
{
    ReCount = Size;
    Received = false;
    HAL_StatusTypeDef result = HAL_UART_Receive_IT(&THE_DEBUG_UART_HAND, ReBuffer, Size);
    if(result == HAL_OK)
    {
        ReCount = Size;
    }
    return result;
}

HAL_StatusTypeDef My_Debug::receiveToIdle(uint32_t Timeout)
{
    Received = false;
    HAL_StatusTypeDef result = HAL_UARTEx_ReceiveToIdle(&THE_DEBUG_UART_HAND, ReBuffer, sizeof(ReBuffer), &ReCount, Timeout);
    if(result == HAL_OK)
    {
        Received = true;
    }
    return result;
}

HAL_StatusTypeDef My_Debug::receiveToIdle_DMA()
{    
    Received = false;
    HAL_StatusTypeDef result = HAL_UARTEx_ReceiveToIdle_DMA(&THE_DEBUG_UART_HAND, ReBuffer, sizeof(ReBuffer));

    return result;
}


Debug_Mode debug_mode = Debug_Mode::normal;


